package com.handinfo.android.uicontrols.controls;

import android.graphics.Bitmap;
import android.graphics.PointF;

import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.utils.Tools;
/**
 * 虚拟键盘控件
 * @author handinfo
 */
public class DWVirtualKey extends DWControl
{
	private Bitmap img_tray = null;//虚拟键盘背景
	private Bitmap img_key = null;//虚拟键盘按键
	private float m_key_x = 0;
	private float m_key_y = 0;
	public boolean m_is_open = false;//是否开启虚拟键了
	public DWVirtualKey(Bitmap tray, Bitmap key) {
		this(tray, key, 0, 0);
	}
	
	public DWVirtualKey(Bitmap tray, Bitmap key, int x, int y) {
		if (tray == null) {
			throw new NullPointerException("DWVirtualKey tray is null!");
		}
		if (key == null) {
			throw new NullPointerException("DWVirtualKey key is null!");
		}
		img_tray = tray;
		img_key = key;
		
		setPoint(x, y);
		setShowWideHigh(img_tray.getWidth(), img_tray.getHeight());
		
		m_key_x = m_show_w / 2;
		m_key_y = m_show_h / 2;
	}
	
	public void render(DWGraphics g)
	{
		setClip(g);
		g.drawBitmap(img_tray, m_show_x, m_show_y, Tools.LEFT_TOP);
		g.drawBitmap(img_key, m_show_x + (int)m_key_x, m_show_y + (int)m_key_y, Tools.HCENTER_VCENTER);
	}

	private boolean changeKey(float x, float y) {
		int w = img_tray.getWidth() / 2;
		int r = w - img_key.getWidth() / 2;
		x -= (m_show_x + w);
		y -= (m_show_y + w);
		if (Math.abs(x * x + y * y) <= r * r) {
			m_key_x = x + w;
			m_key_y = y + w;
		} else {
			m_key_x = (int)(r * x / Math.hypot(x, y)) + w;
			m_key_y = (int)(r * y / Math.hypot(x, y)) + w;
		}
		if (m_listener != null) {
			m_listener.OnClick();
		}
		return false;
	}
	
	public void logic()
	{
		
	}
	/**
	 * 得到虚拟摇杆的偏移值x
	 * @return
	 */
	public float getOffsetX() {
		return m_key_x - m_show_w / 2;
	}
	/**
	 * 得到虚拟摇杆的偏移值y
	 * @return
	 */
	public float getOffsetY() {
		return m_key_y - m_show_h / 2;
	}
	
	public boolean doFling(PointF start, PointF end, float velocityX, float velocityY)
	{
		if (m_is_open) {
			m_key_x = m_show_w / 2;
			m_key_y = m_show_h / 2;
			m_is_open = false;
			if (m_listener != null) {
				m_listener.OnDoubleClick();
			}
			return true;
		}
		return false;
	}
	
	public boolean doDrag(PointF start, PointF end, float velocityX, float velocityY)
	{
		if (m_is_open) {
			m_key_x = m_show_w / 2;
			m_key_y = m_show_h / 2;
			m_is_open = false;
			if (m_listener != null) {
				m_listener.OnDoubleClick();
			}
			return true;
		}
		return false;
	}
	
	public boolean doLongPress(PointF point)
	{
		if (m_is_open) {
			return changeKey(point.x, point.y);
		}
		return false;
	}
	
	public boolean doLongScroll(PointF start, PointF end, float distanceX, float distanceY)
	{
		if (m_is_open) {
			return changeKey(end.x, end.y);
		}
		return false;
	}
	public boolean doScroll(PointF start, PointF end, float distanceX, float distanceY)
	{
		if (m_is_open) {
			return changeKey(end.x, end.y);
		}
		return false;
	}

	public boolean doDown(PointF point)
	{
		if (checkPointOnRect(point.x, point.y)) {
			m_is_open = true;
			return true;
		}
		return false;
	}
	public boolean doClick(float x, float y)
	{
		if (m_is_open) {
			m_key_x = m_show_w / 2;
			m_key_y = m_show_h / 2;
			m_is_open = false;
			if (m_listener != null) {
				m_listener.OnDoubleClick();
			}
			return true;
		}
		return false;
	}

}
